class httpRequest {
  constructor() {}
  // 提交表单数据
  i_posting = false;
  // 延迟展示loading的时间（单位毫秒）
  i_loading_delay = 1000;

  request(method = 'get', url, data = '', s_loading, handleError = true) {
    // 重复提交表单数据，直接返回
    if (this.i_posting) {
      return Promise.reject({
        erroeMessage: '重复提交表单数据',
      });
    }
    return new Promise((resolve, reject) => {
      const app = getApp();
      const qrId = wx.getStorageSync('qrId')
      const partnerId = wx.getStorageSync('partnerId')
      if (app) {
        let token = app.globalData.token;
        if (method === 'post') {
          this.i_posting = true;
        }
        // 正在请求数据
        let loading = true;
        // 展示loading
        if (s_loading) {
          setTimeout(() => {
            if (loading) {
              wx.showLoading({
                title: '',
                mask: true
              });
            }
          }, this.i_loading_delay);
        }
        // 请求数据
        wx.request({
          url: url,
          method: method,
          data: 
          // qrId ? {
          //   ...data,
          //   qrId
          // } : 
          {
            ...data
          },
          timeout: 60000,
          header: {
            'Authorization': 'Bearer ' + token,
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: (res) => {
            if (res.data.success) {
              //后端未返值，默认空对象
              if (res.data === undefined) {
                res.data = {}
              }
              resolve(res.data)
            } else {
              if (res.data.success === false) {
                if (handleError) {
                  wx.showModal({
                    title: '错误信息',
                    content: res && res.data && res.data.message || '暂无信息',
                    showCancel: false,
                  });
                }
                reject(res);
              } else {
                if (res.statusCode == '401') {
                  return app.clearCache().then(() => {
                    return this.request(method, url, data, s_loading, handleError).then((r) => {
                      resolve(r);
                    }).catch((r) => {
                      reject(r);
                    });
                  });
                }
                //后端服务异常
                if (res.statusCode == '500' || res.statusCode == '503') {
                  wx.showToast({
                    title: '接口请求异常，请稍后重试',
                    icon: 'none'
                  })
                }
                if (res.statusCode == '502') {
                  wx.showModal({
                    title: '提示',
                    content: '请检查网络或稍后重试',
                  })
                } else {
                  wx.showModal({
                    title: '提示',
                    content: '请求异常，请联系管理员',
                  });
                }
              }
              reject(res)
            }
          },
          fail: (err) => {
            wx.showToast({
              title: '数据请求失败，请检查网络或返回重试',
              icon: 'none',
              duration: 4000
            })
            reject(err)
          },
          complete: () => {
            loading = false;
            this.i_posting = false;
            wx.hideLoading();
          },
        });
      }

    });
  }
}

export default httpRequest